#!usr/bin/env python3
# -*- coding: utf-8 -*-

'''
pygal 是一个制作图表的库
'''
import os
from random import randint
from utils.commonUtil import set_cn_font
import pygal

set_cn_font()

class Die():
    def __init__(self,num_sides=6):
        self.num_sides = num_sides
    
    def roll(self):
        return randint(1,self.num_sides)

root = os.path.dirname(__file__)

d6 = Die()

data = []

data_count = 100

for i in range(0,data_count):
    data.append(d6.roll())

frequencies = []
die_nums = []
for i in range(1,d6.num_sides+1):
    die_nums.append(i)
    frequencies.append(data.count(i))

#print(data,frequency)

#根据数据绘制直方图

hist = pygal.Bar() #直方图
hist = pygal.StackedBar() # 重叠显示的 直方图 
hist.title = "随机掷{0}次色字,各个数字出现的频率".format(data_count)
hist.x_labels = die_nums
hist.x_title = "掷出的点数"
hist.y_title = "对应点数出现的频率"

hist.add("D6",frequencies)
hist.add("假设",frequencies)

hist.render_to_file(os.path.join(root,"bar.svg"))
#hist.render_to_png(os.path.join(root,"bar.png"))

# 更多关于pygal的api 查看官网文档(这个非常有趣 有空要看 2017年6月22日10:50:57) http://www.pygal.org/en/stable/documentation/index.html 
